package cn.edu.ctbu.gudie.service;

import cn.edu.ctbu.gudie.domain.*;
import cn.edu.ctbu.gudie.repository.*;
import cn.edu.ctbu.gudie.service.Interface.DoctorServiceIn;
import cn.edu.ctbu.gudie.service.Interface.PatientServiceIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DoctorService implements DoctorServiceIn {
    @Autowired
    private DoctorRepository doctorRepository;
    @Autowired
    private AppointmentRepository appointmentRepository;
    /**
     * 获取所有医生信息
     * @param pageable 分页
     * @return
     */
    @Override
    public Page<Doctor> findAll(Pageable pageable) {

        return doctorRepository.findAll(pageable);
    }
    @Override
    public boolean deleteAppointment(Integer id) {
        if (appointmentRepository.existsById(id)) {
            appointmentRepository.deleteById(id);
            return true;
        }
        return false;
    }
    @Override
    public Page<Appointment> getAllAppointments(Pageable pageable) {
        return appointmentRepository.findAll(pageable); // 获取所有预约记录
    }
//    /**
//     * 获取所有医生信息
//     * @param pageable 分页
//     * @return
//     */
//    @Override
//    public Page<Advice> getAllAdvices(Pageable pageable) {
//
//        return adviceRepository.getAllAdvices(pageable);
//    }

    /**
     * 通过姓名来查询对应的医生列表
     *
     * @param patientName 指定姓名的医生列表
     * @return
     */
//    @Override
//    public List<Patient> findByPatientName(String patientName) {
//        return patientRepository.findByPatientName(patientName);
//    }
//    /**
//     * 找出所有符号指定姓名和密码的医生信息
//     * @param patientName   姓名
//     * @param patientPassword   密码
//     * @return 医生信息
//     */
//    @Override
//    public List<Patient> findByPatientNameAndPatientPassword(String patientName, String patientPassword) {
//        return patientRepository.findByPatientNameAndPatientPassword(patientName, patientPassword);
//    }
//    public Map<String, Object> recommendDepartmentsAndHospitals(List<Integer> tagIds) {
//        Set<Integer> departmentIds = new HashSet<>();
//
//        // 找到匹配的科室
//        for (Integer tagId : tagIds) {
//            List<DepartmentTag> departmentTags = departmentTagRepository.findByTagId(tagId);
//            for (DepartmentTag departmentTag : departmentTags) {
//                departmentIds.add(departmentTag.getDepartmentId());
//            }
//        }
//
//        List<Departments> departments = departmentsRepository.findAllById(departmentIds);
//
//        // 根据科室推荐医院
//        List<Hospitals> hospitals = new ArrayList<>();
//        for (Integer departmentId : departmentIds) {
//            List<DepartmentHospital> departmentHospitals = departmentHospitalRepository.findByDepartmentId(departmentId);
//            for (DepartmentHospital departmentHospital : departmentHospitals) {
//                Optional<Hospitals> hospital = hospitalsRepository.findById(departmentHospital.getHospitalId());
//                hospital.ifPresent(hospitals::add);
//            }
//        }
//
//        // 按照评分排序医院
//        hospitals.sort(Comparator.comparingDouble(Hospitals::getScore).reversed());
//
//        Map<String, Object> recommendations = new HashMap<>();
//        recommendations.put("departments", departments);
//        recommendations.put("hospitals", hospitals);
//        return recommendations;
//    }
//
//    /**
//     * 保存医生信息
//     * @param patient
//     * @return
//     */
//    @Override
//    public Patient save(Patient patient) {
//        return patientRepository.save(patient);
//    }
//    /**
//     * 保存医生信息
//     * @param advice
//     * @return
//     */
//    @Override
//    public Advice saveAdvice(Advice advice) {
//        return adviceRepository.save(advice);
//    }
//    @Override
//    public void deleteAdvice(Integer id) {
//        adviceRepository.deleteById(id);
//    }
}
